package com.smartbus.bus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.smartbus.common.annotation.Log;
import com.smartbus.common.core.controller.BaseController;
import com.smartbus.common.core.domain.AjaxResult;
import com.smartbus.common.enums.BusinessType;
import com.smartbus.bus.domain.AnnouncementLogs;
import com.smartbus.bus.service.IAnnouncementLogsService;
import com.smartbus.bus.service.IBusStationsService;
import com.smartbus.bus.service.IAnnouncementTypesService;
import com.smartbus.bus.service.IVoiceFilesService;
import com.smartbus.bus.domain.BusStations;
import com.smartbus.bus.domain.AnnouncementTypes;
import com.smartbus.bus.domain.VoiceFiles;
import com.smartbus.common.utils.poi.ExcelUtil;
import com.smartbus.common.core.page.TableDataInfo;

/**
 * 报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/bus/announcement-logs")
public class AnnouncementLogsController extends BaseController
{
    @Autowired
    private IAnnouncementLogsService announcementLogsService;

    @Autowired
    private IBusStationsService busStationsService;

    @Autowired
    private IAnnouncementTypesService announcementTypesService;

    @Autowired
    private IVoiceFilesService voiceFilesService;

    /**
     * 填充报站日志的关联信息
     */
    private void enrichAnnouncementLogInfo(AnnouncementLogs log) {
        // 填充站点名称
        if (log.getStationId() != null) {
            try {
                BusStations station = busStationsService.selectBusStationsById(log.getStationId());
                if (station != null) {
                    log.setStationName(station.getStationName());
                }
            } catch (Exception e) {
                logger.warn("获取站点名称失败，stationId: " + log.getStationId(), e);
                log.setStationName("未知站点");
            }
        }

        // 填充报站类型名称
        if (log.getAnnouncementTypeId() != null) {
            try {
                AnnouncementTypes type = announcementTypesService.selectAnnouncementTypesById(log.getAnnouncementTypeId());
                if (type != null) {
                    log.setAnnouncementTypeName(type.getTypeName());
                }
            } catch (Exception e) {
                logger.warn("获取报站类型失败，announcementTypeId: " + log.getAnnouncementTypeId(), e);
                log.setAnnouncementTypeName("未知类型");
            }
        }

        // 填充语音文件路径
        if (log.getVoiceFileId() != null) {
            try {
                VoiceFiles voiceFile = voiceFilesService.selectVoiceFilesById(log.getVoiceFileId());
                if (voiceFile != null) {
                    log.setVoiceFilePath(voiceFile.getFilePath());
                }
            } catch (Exception e) {
                logger.warn("获取语音文件失败，voiceFileId: " + log.getVoiceFileId(), e);
            }
        }

        // TODO: 添加公交车信息和线路信息的填充
        // 这需要相应的 IBusService 和 IRouteService
        if (log.getBusId() != null) {
            log.setBusPlateNumber("公交车-" + log.getBusId()); // 临时显示
        }

        if (log.getRouteId() != null) {
            log.setRouteName("线路-" + log.getRouteId()); // 临时显示
        }
    }

    /**
     * 查询报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查列表
     */
    @PreAuthorize("@ss.hasPermi('bus:logs:list')")
    @GetMapping("/list")
    public TableDataInfo list(AnnouncementLogs announcementLogs)
    {
        startPage();
        List<AnnouncementLogs> list = announcementLogsService.selectAnnouncementLogsList(announcementLogs);

        // 填充关联信息
        list.forEach(this::enrichAnnouncementLogInfo);

        return getDataTable(list);
    }

    /**
     * 导出报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查列表
     */
    @PreAuthorize("@ss.hasPermi('bus:logs:export')")
    @Log(title = "报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AnnouncementLogs announcementLogs)
    {
        List<AnnouncementLogs> list = announcementLogsService.selectAnnouncementLogsList(announcementLogs);
        ExcelUtil<AnnouncementLogs> util = new ExcelUtil<AnnouncementLogs>(AnnouncementLogs.class);
        util.exportExcel(response, list, "报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查数据");
    }

    /**
     * 获取报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查详细信息
     */
    @PreAuthorize("@ss.hasPermi('bus:logs:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AnnouncementLogs log = announcementLogsService.selectAnnouncementLogsById(id);
        if (log != null) {
            enrichAnnouncementLogInfo(log);
        }
        return success(log);
    }

    /**
     * 新增报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查
     */
    @PreAuthorize("@ss.hasPermi('bus:logs:add')")
    @Log(title = "报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AnnouncementLogs announcementLogs)
    {
        return toAjax(announcementLogsService.insertAnnouncementLogs(announcementLogs));
    }

    /**
     * 修改报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查
     */
    @PreAuthorize("@ss.hasPermi('bus:logs:edit')")
    @Log(title = "报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AnnouncementLogs announcementLogs)
    {
        return toAjax(announcementLogsService.updateAnnouncementLogs(announcementLogs));
    }

    /**
     * 删除报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查
     */
    @PreAuthorize("@ss.hasPermi('bus:logs:remove')")
    @Log(title = "报站记录日志，记录每次报站的详细信息，用于运营分析和故障排查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(announcementLogsService.deleteAnnouncementLogsByIds(ids));
    }
}
